package controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import util.MenuItem;

@ManagedBean
@SessionScoped
public class MenuController implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<MenuItem> menuItems;
	private String url;
	private String meniuURL;
	private int nr;
	
	/**
	 * User rank:	0: Admin
	 * 				1: Tester
	 * 				2: Candidate
	 * */
	private int userRank;
	
	/**
	 * True if a user is logged in.
	 * */
	private boolean loggedIn;

	@PostConstruct
	public void initMenuItems() {
		loggedIn = false;
		userRank = -1;
		menuItems = new ArrayList<MenuItem>();
		nr=0;
	}
	
	public void redirect(){
		try {
			FacesContext
			.getCurrentInstance()
			.getExternalContext()
			.redirect("/MSGCandidateTesting/faces/employeeAuthentification.xhtml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void redirect2(){
			clearMenuItems();
			addMenuItem("editAdmin.xhtml", "Administrators");
			addMenuItem("alocateTesterUser.xhtml", "Testers");
			addMenuItem("editCandidates.xhtml", "Candidates");
			addMenuItem("editDomains.xhtml", "Domains - Categories");
			addMenuItem("editQuestions.xhtml", "Questions");
			addMenuItem("roomPage.xhtml", "Rooms");
			addMenuItem("logOut.xhtml", "Log out");
			setMeniuURL(meniuURL);

	}

	public void setMenuItems(List<MenuItem> menuItems) {
		this.menuItems = menuItems;
	}

	public List<MenuItem> getMenuItems() {
		return menuItems;
	}

	public void clearMenuItems() {
		menuItems.clear();
	}

	public void addMenuItem(String link, String name) {
		menuItems.add(new MenuItem(link, name));
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
//		HttpServletRequest request = (HttpServletRequest) FacesContext
//				.getCurrentInstance().getExternalContext().getRequest();
//		String link;
//		link = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/faces/" + url;
		String t[]=url.split("/");
		meniuURL=t[t.length-1];
		//System.out.println("MenuController: " + meniuURL);
		
		if(nr==2)
			nr=0;
		nr++;
		
		this.url = url;
	}

	
	
	public int getNr() {
		return nr;
	}

	public void setNr(int nr) {
		this.nr = nr;
	}

	public String getMeniuURL() {
		return meniuURL;
	}

	public void setMeniuURL(String meniuURL) {
		this.meniuURL = meniuURL;
	}

	public void setUserRank(int userRank) {
		this.userRank = userRank;
	}

	public int getUserRank() {
		return userRank;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}
	
	

}
